﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BSCMFF.Core.DataAccessLayer;
using Telerik.Web.UI;

namespace BSCMFF.Web.UserControls.EmployeeTabs
{
    public partial class SurveysTab : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int userID = (int)Session["loggedUserID"];

            if (!IsPostBack)
            {
                BSCEntities context = new BSCEntities();

                //List<Survey_Form> sfs = (from sfuser in context.Survey_User.Include("Survey_Form")
                //                         where sfuser.User_FK == userID
                //                         orderby sfuser.Survey_Form.CreationDate descending
                //                         select sfuser.Survey_Form).ToList();

                List<Survey_User> sfs = (from sfuser in context.Survey_User.Include("Survey_Form")
                                         where sfuser.User_FK == userID
                                         orderby sfuser.Survey_Form.CreationDate descending
                                         select sfuser).ToList();



                RadGridSurveys.DataSource = sfs;
                RadGridSurveys.DataBind();
            }
        }

        protected void RadGridSurveys_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == "Fill")
            {
                GridEditableItem editedItem = e.Item as GridEditableItem;
                int sfID = Convert.ToInt32(editedItem.OwnerTableView.DataKeyValues[editedItem.ItemIndex]["Survey_Form.ID"]);

                Response.Redirect("../Surveys/FillSurvey.aspx?Survey_FormID=" + sfID.ToString());
            }
        }

        protected void RadGridSurveys_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)
            {
                GridDataItem item = (GridDataItem)e.Item;

                if (((CheckBox)item["Started"].Controls[0]).Checked == false ||
                    ((CheckBox)item["Finished"].Controls[0]).Checked == true ||
                    ((CheckBox)item["Filled"].Controls[0]).Checked == true)
                {
                    item["Fill"].Visible = false;
                }
            }
        }
    }
}